<?php
// +----------------------------------------------------------------------
// | QSCMS
// +----------------------------------------------------------------------
// | Copyright (c)  2025 https://www.qqss.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed QSCMS is not free software, commercial use must purchase official license from official website to avoid unnecessary legal disputes.
// +----------------------------------------------------------------------
// | Author: Contract 990504246@qq.com
// +----------------------------------------------------------------------

namespace app\common\util;

use Overtrue\Pinyin\Pinyin;

/**
 * 拼音转换工具类
 */
class PinyinHelper
{
    /**
     * 将中文转换为拼音
     * @param string $chinese 中文字符串
     * @param string $separator 分隔符
     * @return string
     */
    public static function toPinyin($chinese, $separator = '')
    {
        return (new Pinyin())->permalink($chinese, $separator);
    }

    /**
     * 获取中文字符串的首字母
     * @param string $chinese 中文字符串
     * @param string $separator 分隔符
     * @return string
     */
    public static function getFirstLetter($chinese, $separator = '')
    {
        return (new Pinyin())->abbr($chinese)->join($separator);
    }

    /**
     * 生成URL友好的pinyin
     * @param string $text 原始文本
     * @param int $maxLength 最大长度
     * @return string
     */
    public static function generatePinyin($text, $maxLength = 100)
    {
        // 先转换为拼音
        $pinyin = self::toPinyin($text, '');

        // 转换为小写
        $pinyin = strtolower($pinyin);

        // 移除特殊字符，只保留字母、数字和连字符
        $pinyin = preg_replace('/[^a-z0-9\-]/', '', $pinyin);

        // 移除多余的连字符
        $pinyin = preg_replace('/-+/', '-', $pinyin);

        // 移除首尾的连字符
        $pinyin = trim($pinyin, '-');

        // 限制长度
        if (strlen($pinyin) > $maxLength) {
            $pinyin = substr($pinyin, 0, $maxLength);
            $pinyin = rtrim($pinyin, '-');
        }

        // 如果为空，生成随机字符串
        if (empty($pinyin)) {
            $pinyin = 'item-' . uniqid();
        }

        return $pinyin;
    }

    /**
     * 检查pinyin是否唯一
     * @param string $pinyin
     * @param string $table 表名
     * @param int $excludeId 排除的ID
     * @return bool
     */
    public static function isPinyinUnique($pinyin, $table, $excludeId = null)
    {
        $query = \think\facade\Db::table($table)->where('pinyin', $pinyin);

        if ($excludeId) {
            $query->where('id', '<>', $excludeId);
        }

        return $query->count() === 0;
    }

    /**
     * 生成唯一的pinyin
     * @param string $text 原始文本
     * @param string $table 表名
     * @param int $excludeId 排除的ID
     * @return string
     */
    public static function generateUniquePinyin($text, $table, $excludeId = null)
    {
        $pinyin  = self::generatePinyin($text);
        $counter = 1;

        while (!self::isPinyinUnique($pinyin, $table, $excludeId)) {
            $pinyin = $pinyin . $counter;
            $counter++;
        }

        return $pinyin;
    }
}
